package com.ningyue.water.service.messagedeal.impl;

import com.ningyue.water.entity.WaterMeasureRecordDO;
import com.ningyue.water.entity.common.Result;
import com.ningyue.water.enums.MeasureTypeEnums;
import com.ningyue.water.enums.UnitEnums;
import com.ningyue.water.service.messagedeal.MessageDealService;
import com.ningyue.water.service.watermeasure.WaterMeasureService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

/**
 * Created by steven on 16/1/5.
 */
@Service
public class MessageDealServiceImpl implements MessageDealService {

    @Autowired
    private WaterMeasureService waterMeasureService;
    @Override
    public Result<?> dealMessage(byte[] message) {
        Integer frameLen=message.length;
        Integer pos=0;
        Byte messageType=message[pos];
        pos++;
        List<Byte> remainLenList=new ArrayList<Byte>();
        remainLenList.add(message[pos]);
        while (message[pos]>>7==1) {
            pos++;
            remainLenList.add(message[pos]);
        }
        pos++;
        Byte topicLenMsb=message[pos];
        pos++;
        Byte topicLenLsb=message[pos];
        int topicLength=topicLenMsb*16+topicLenLsb;
        pos++;
        List<Byte> topicByteList=new ArrayList<Byte>();
        for(int i=0;i<topicLength;i++){
            topicByteList.add(message[pos+i]);

        }
        pos=pos+topicLength;
        Byte piMsb=message[pos];
        pos++;
        Byte piLsb=message[pos];
        List<Byte> payloadList=new ArrayList<Byte>();
        pos++;
        while (pos<frameLen){

            payloadList.add(message[pos]);
            pos++;
        }
        System.out.println("haliluya");
        if(payloadList.size()>0){
            return Result.wrapSuccessfulResult(payloadList);
        }else {
            return Result.wrapErrorResult("","no payload data!");
        }
    }

    @Override
    public Result<?> dealFrame(List<Byte> frame) {
        if(frame.get(0).equals(0x45)){
            processMeasureMessage(frame);
        }else if(frame.get(0).equals(0x55)){
            processAlarmMessage(frame);
        }
        return null;
    }

    private Boolean processMeasureMessage(List<Byte> frame){
        WaterMeasureRecordDO waterMeasureRecordDO=new WaterMeasureRecordDO();
        waterMeasureRecordDO.setGmtCreate(new Date());
        waterMeasureRecordDO.setGmtModify(new Date());
        waterMeasureRecordDO.setIsDeleted("N");
        waterMeasureRecordDO.setDeviceCompany(1);
        waterMeasureRecordDO.setDeviceOwner(1);
        waterMeasureRecordDO.setDataName("一级反渗透水压");
        waterMeasureRecordDO.setDataUnit(UnitEnums.PRESSURE.getName());
        waterMeasureRecordDO.setDataValue(120.0);
        waterMeasureRecordDO.setDataType(MeasureTypeEnums.PRESSURE.getKey());
        Integer affectline= waterMeasureService.addWaterMeasureRecord(waterMeasureRecordDO);
        if(affectline!=1){
            return false;
        }else{
            return true;
        }
    }

    private Boolean processAlarmMessage(List<Byte> frame){
        return null;
    }
}
